﻿using System;
using System.Web.Mvc;
using ZhongheCms.Models;
using GsnHelper;
namespace ZhongheCms.Controllers
{
    public class BaseInfoController : Controller
    {
      
        public ActionResult Index(int id=0,string m="",string f="",string p="",string fid="",string r="")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry =
                GswTblBaseInfo.Fetch("where FunctionId=" + fid + " and CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " order by TypeNo");

            return View(qry);
        }

        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var baseInfo = id == 0
                ? new GswTblBaseInfo
                {
                    CustomerId = GswTblUser.CurrentUser.CustomerId,
                    MasterType = "",
                    FunctionId = InputValid.StrToInt(fid, 0),
                    FunctionName = f,
                    TypeNo = 1
                }
                : GswTblBaseInfo.SingleOrDefault(id);
            if (id != 0) return View(baseInfo);
            var qry =
                GswTblBaseInfo.FirstOrDefault("select max(TypeNo) as TypeNo from GswTblBaseInfo where CustomerId=" +
                                              GswTblUser.CurrentUser.CustomerId + " and FunctionId=" + fid);
            if (qry != null)
            {
                baseInfo.TypeNo = qry.TypeNo + 1;
            }
            return View(baseInfo);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (GswTblBaseInfo.SingleOrDefault("where MasterId=0 and CustomerId=" +GswTblUser.CurrentUser.CustomerId + " and MasterType='" + c["MasterType"] + "' and Id<>" + id + "") != null)
                ModelState.AddModelError(string.Empty, c["MasterType"].Trim() + "已经存在");
            var baseInfo = id == 0 ? new GswTblBaseInfo() : GswTblBaseInfo.SingleOrDefault(id);
            baseInfo.CustomerId = GswTblUser.CurrentUser.CustomerId;
            baseInfo.MasterType = c["MasterType"];
            baseInfo.FunctionId = InputValid.StrToInt(fid, 0);
            baseInfo.FunctionName = f;
            baseInfo.TypeNo = InputValid.StrToInt(c["TypeNo"],0);
            baseInfo.SubType ="";
            baseInfo.Remark = c["Remark"];
            baseInfo.TypePic = c["TypePic"];
            baseInfo.NewFlag = InputValid.StrToInt(c["NewFlag"], 0);
            baseInfo.RelateId = InputValid.StrToInt(r, 0);
            try
            {
                if (ModelState.IsValid)
                {
                   
                    if (id == 0)
                    {
                        baseInfo.Insert();
                        baseInfo.MasterType = "";
                        baseInfo.Remark = "";
                        baseInfo.TypePic = "";
                        baseInfo.TypeNo++;
                        baseInfo.NewFlag = 0;
                        baseInfo.Id = 0;
                    }
                    else
                    {
                        baseInfo.Update();
                        GswTblBaseInfo.Update("set MasterType='" + baseInfo.MasterType + "' where MasterId=" +
                                              baseInfo.Id);
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            HttpContext.Response.StatusCode = 303;
            return View(baseInfo);
            
        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "", string r = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblBaseInfo.Delete(id);
            var qry =
                 GswTblBaseInfo.Fetch("where FunctionId=" + fid + " and CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                      " order by TypeNo");

        
            return View("Index", qry);
        }
    }
}
